import _ from 'lodash'
/**
 * @description: 二维鹰眼地图功能
 * @param {*}
 * @return {*}
 */
class HawkEye2DMap {
  constructor(viewer, _this) {
    // 主图
    this._viewer = viewer;
    // 鹰眼图
    this._hawkEyeMap = null;

    this._this = _this;

  }

  // 初始化函数
  _init() {
    this._divInit();
    this._mapInit();
  }

  // 动态创建div,及div初始化
  _divInit() {}

  gettypeczmcbj(type) {
    let gettypeczmcbjres = [];
    this._gettypeczmcbj(earth.sceneTree.root.children, type, gettypeczmcbjres);
    return gettypeczmcbjres;
  }

  _gettypeczmcbj(arr, type, gettypeczmcbjres) {
    arr.map((item) => {
      if (_.has(item, "children")) {
        this._gettypeczmcbj(item.children, type, gettypeczmcbjres);
      }
      if (_.has(item, "czmObject") && item.czmObject.xbsjType === type) {
        gettypeczmcbjres.push(item);
      }
    });
  }

  // 初始化地图
  _mapInit() {
    this._hawkEyeMap = new Cesium.Viewer("hawkEye2dMap", {
      geocoder: false,
      homeButton: false,
      sceneModePicker: false,
      baseLayerPicker: false,
      navigationHelpButton: false,
      animation: false,
      timeline: false,
      fullscreenButton: false,
      // 鹰眼地图中设置为二维地图
      sceneMode: Cesium.SceneMode.SCENE2D,
    });
    this._hawkEyeMap.cesiumWidget.creditContainer.style.display = "none";
    this._hawkEyeMap.scene.backgroundColor = Cesium.Color.TRANSPARENT;
    this._hawkEyeMap.imageryLayers.removeAll();
    // this._hawkEyeMap.scene.backgroundColor = Cesium.Color.fromCssColorString("#D3EBFF")
    this._hawkEyeMap.scene.globe.baseColor =
      Cesium.Color.fromCssColorString("#D3EBFF");
    // 是否支持图像渲染像素化处理
    if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
      this._hawkEyeMap.resolutionScale = window.devicePixelRatio;
    }

    // 开启抗锯齿
    this._hawkEyeMap.scene.postProcessStages.fxaa.enabled = true;
    // 鹰眼图中添加高德路网中文注记图（鹰眼图中坐标偏移一点不影响）
    this._hawkEyeMap.imageryLayers.addImageryProvider(
      new Cesium.UrlTemplateImageryProvider({
        url: '/tiles/img_c/{oz}/{x}/{y}',
        customTags: {
            oz(imageProvider, x, y, level) {
                return level + 1
            },
        },
        tileHeight: 256,
        tileWidth: 256,
        tilingScheme: new Cesium.GeographicTilingScheme({}),
      })
    );

    // 引起事件监听的相机变化幅度
    this._viewer.camera.percentageChanged = 0.02;
    this._hawkEyeMap.camera.percentageChanged = 0.5;

    this._bindEvent();

    let _this = this;

    const pointe = {
      position: Cesium.Cartesian3.fromDegrees(109.44, 32.11, 30.0),
      point: {
        show: true, // 是否展示
        pixelSize: 10, // 点的大小
        // heightReference:HeightReference.NONE,//相对高度
        color: new Cesium.CallbackProperty(function color(time, result) {
          var txf = Math.floor(time.secondsOfDay);
          // var result = ''
          if (txf % 2 === 0) {
            // 间隔两秒变换
            result = Cesium.Color.RED;
          } else {
            result = Cesium.Color.YELLOW;
          }
          return result;
        }, false), // 颜色
        outlineColor: Cesium.Color.SKYBLUE, // 边框颜色
        outlineWidth: 1, // 边框宽度
        // scaleByDistance: new Cesium.NearFarScalar(100, 1, 100000, 0), //缩放距离设置
        // translucencyByDistance:new Cesium.NearFarScalar(100, 0.2, 10000, 0.8),//点的半透明设置
        // distanceDisplayCondition:new Cesium.DistanceDisplayCondition(1000, 10000),//点的显隐距离
        // disableDepthTestDistance:5000000,//禁用深度测试距离
      },
    };

    this._pe = this._hawkEyeMap.entities.add(pointe);
  }
  // 绑定事件
  _bindEvent() {
    // 但当鹰眼图为二维地图时，则不能直接设置
    this._viewCenter = new Cesium.Cartesian2(
      // Math.floor取整函数
      Math.floor(this._viewer.canvas.clientWidth / 2),
      Math.floor(this._viewer.canvas.clientHeight / 2)
    );
    // 鹰眼与主图同步
    this._viewer.camera.changed.addEventListener(this._syncEyeMap, this);
    // 第一次刷新渲染时联动
    this._viewer.scene.preRender.addEventListener(this._syncEyeMap, this);
  }

  // 同步主图与鹰眼地图
  _syncEyeMap() {
    // 监听主图
    // new Cesium.ScreenSpaceEventHandler(this._viewer.canvas).setInputAction(
    //   () => {
    //     this._isMainMapTrigger = true;
    //     this._isEyeMapTrigger = false;
    //   },
    //   Cesium.ScreenSpaceEventType.MOUSE_MOVE
    // );

    // pickEllipsoid用于将屏幕坐标转换为世界坐标
    this._viewCenterPos = this._viewer.scene.camera.pickEllipsoid(
      this._viewCenter
    );
    if (!this._viewCenterPos) {
      return false;
    }

    // console.log(this._pe.position)

    // 3996965.9465706674

    // 判断视角是否在地下
    // 不在的话取屏幕中心点；在的话取相机位置
    this._pe.position = this._viewCenterPos;

    this._hudu = Cesium.Cartographic.fromCartesian(
      this._viewer.scene.camera.position
    );

    // 地下模式位置错误
    if (this._hudu.height < 10) {
      this._viewCenterPos = this._viewer.scene.camera.position;
      this._pe.position = this._viewer.scene.camera.position;
    }

    //判断鹰眼图是否显示
    // if (this._hudu.height < 10000) {
    //   window.yingyancom.showyingyan = true;
    // } else {
    //   window.yingyancom.showyingyan = false;
    // }

    // postionWC：标准世界坐标系坐标
    this._distance = Cesium.Cartesian3.distance(
      this._viewCenterPos,
      this._viewer.scene.camera.positionWC
    );
    this._hawkEyeMap.scene.camera.lookAt(
      this._viewCenterPos,
      new Cesium.Cartesian3(0.0, 0.0, this._distance + 25000)
    );
  }
}

export default HawkEye2DMap;
